package views;

import DAO.ICcardDao;
import Entity.ICcard;
import com.mysql.cj.protocol.Message;
import util.Get_Now_Time;
import util.NotNull;
import util.Result;
import util.RfidTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class IcCardManagement<iCcard> {
    static  final  String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static  final  String DB_URL = "jdbc:mysql://192.168.126.129:20000/tour";
    static  final  String USER= "swpu_user";
    static  final  String PASS = "2121709a*";
    /*添加到数据库所必需的代码*/
    static Connection connect = null;
    static PreparedStatement cmmd = null;
    static ResultSet rs = null;
    static String sql = "";


    JLabel l4;
    JLabel l1;
    JLabel l2;
    JLabel l3;
    JLabel l5;
    JLabel l6;
    JLabel l7;
    JTextField IcID;   //Ic卡编号
    JTextField Icstatu;  //入库卡状态
    JTextField IcDate;   //发卡日期
    JTextField IcDstr;   //报损日期
    JTextField cardCreat;   //发卡人
    JTextField Iccard;      //Ic卡卡号
    JTextField IDrest;      //余额
    JButton btnOK;           //保存
    JButton btn3;            //重置
    JButton breturn;
    int flag;
    ICcard iCcard = new ICcard();
    ICcardDao iCcardDao = new ICcardDao();


    class PanelBg extends  JPanel{
        Dimension d;
        Image image;
        PanelBg(Dimension d,Image image){
            this.d=d;
            this.image=image;
        }
        @Override
        public void paintComponent(Graphics g){
            g.drawImage(image,0,0,d.width,d.height,this);
        }
    }
    Dimension fmsize=new Dimension(600,700);
    ImageIcon imageicon = new ImageIcon(this.getClass().getResource("/erhai4.jpg"));

    IcCardManagement(Result result){
        JFrame jframe = new JFrame();
        jframe.setSize(600,700);
        jframe.setDefaultCloseOperation(jframe.DISPOSE_ON_CLOSE);
        jframe.setLayout(null);
        jframe.setLocationRelativeTo(null);


        IcCardManagement.PanelBg pContent = new IcCardManagement.PanelBg(fmsize,imageicon.getImage());
        pContent.setLayout(null);
        pContent.setBounds(0,0,600,700);
        jframe.getContentPane().add(pContent);



        l1 = new JLabel("Ic卡编号");
        l1.setBounds(100,40,70,30);
        pContent.add(l1);

        IcID = new JTextField();
        IcID.setBounds(180,40,250,30);
        IcID.setEnabled(false);
        pContent.add(IcID);


        l2 = new JLabel("入库卡状态");
        l2.setBounds(100,110,70,30);
        pContent.add(l2);

        Icstatu = new JTextField();
        Icstatu.setBounds(180,110,250,30);
//        String vIcstatu =Icstatu.getText();
//        int vvIcstatu = Integer.valueOf(vIcstatu).intValue();

        pContent.add(Icstatu);

        l3 = new JLabel("发卡日期");
        l3.setBounds(100,180,70,30);
        pContent.add(l3);

        IcDate = new JTextField();
        IcDate.setBounds(180,180,250,30);
//        String vIcDate = new Get_Now_Time().Getnow();
//        IcDate.setText(vIcDate);

        pContent.add(IcDate);


        l4 = new JLabel("报损日期");
        l4.setBounds(100,250,70,30);
        pContent.add(l4);

        IcDstr = new JTextField();
        IcDstr.setBounds(180,250,250,30);
//        String vIcDstr = new Get_Now_Time().Getnow();
//        IcDstr.setText(vIcDstr);

        pContent.add(IcDstr);


        l5 = new JLabel("发卡人");
        l5.setBounds(100,320,70,30);
        pContent.add(l5);

        cardCreat = new JTextField();
        cardCreat.setBounds(180,320,250,30);
        String vcardCreat = cardCreat.getText();
        pContent.add(cardCreat);


        l6 = new JLabel("Ic卡号");
        l6.setBounds(100,390,70,30);
        pContent.add(l6);

        Iccard = new JTextField();
        Iccard.setBounds(180,390,250,30);
        String vIccard  = Iccard.getText();
        pContent.add(Iccard);

        l7 = new JLabel("余额");
        l7.setBounds(100,460,70,30);
        pContent.add(l7);

        IDrest = new JTextField();
        IDrest.setBounds(180,460,250,30);
        String vIDrest = IDrest.getText();
        pContent.add(IDrest);


        if ((result.getStatus().equals("1"))) {
            try {
                        IcID.setText(Integer.valueOf(iCcardDao.justgetID()).intValue()+1+"");
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
            Icstatu.setText("1");
            IcDate.setText(null);
            IcDstr.setText(null);
            flag=1;
        }
        if(result.getStatus().equals("0")){
            IcID.setText((String) result.getMessage());
            String vIcID = IcID.getText();
            iCcard = iCcardDao.get_byID(vIcID);
            Icstatu.setText(String.valueOf(iCcard.getIcstatu()));
            IcDate.setText(iCcard.getIcDate());
            IcDstr.setText(iCcard.getIcDstr());
            cardCreat.setText(iCcard.getCardCreat());
            Iccard.setText(iCcard.getIccard());
            IDrest.setText(iCcard.getIDRest());
            flag=0;
        }

        btn3 =new JButton("保存");
        btn3.setBounds(180,570,70,50);
        btn3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if((new NotNull().IsNull(IcID,Icstatu,Iccard))==false){
                    JOptionPane.showMessageDialog(null,"不能为空");
                    return;
                }
                if (flag==1){
//                    IcID.setText((String) result.getMessage());

                    String vIcID = IcID.getText();
                    int vIcstatu =Integer.valueOf(Icstatu.getText());
                    String vIcDate =IcDate.getText();
                    String vIcDstr = IcDstr.getText();
                    String vcardCreat = cardCreat.getText();
                    String vIccard  = Iccard.getText();
                    String vIDrest = IDrest.getText();
//                    ICcard iCcard = new ICcard();

                    iCcard.setIcID(vIcID);
                    iCcard.setIcstatu(vIcstatu);
                    iCcard.setIcDate(vIcDate);
                    iCcard.setIcDstr(vIcDstr);
                    iCcard.setCardCreat(vcardCreat);
                    iCcard.setIccard(vIccard);
                    iCcard.setIDRest(vIDrest);
                    iCcardDao.insert(iCcard);
                System.out.println(iCcard.getIcID());
                System.out.println(iCcard.getIcstatu());
                JOptionPane.showMessageDialog(null,"新增成功");
                }
                if(flag==0){
                    String vIcID = IcID.getText();
                    int vvIcstatu = Integer.parseInt(Icstatu.getText());
                    String vIcDstr = new String();
                    if(vvIcstatu==4){
                        vIcDstr = new Get_Now_Time().Getnow();
                        IcDstr.setText(vIcDstr);

                    }
                    vIcDstr = IcDstr.getText();
                    String vIcDate = IcDate.getText();
                    String vcardCreat = cardCreat.getText();
                    String vIccard  = Iccard.getText();
                    String vIDrest = IDrest.getText();
                    iCcard.setIcID(vIcID);
                    iCcard.setIcstatu(vvIcstatu);
                    iCcard.setIcDate(vIcDate);
                    iCcard.setIcDstr(vIcDstr);
                    iCcard.setCardCreat(vcardCreat);
                    iCcard.setIccard(vIccard);
                    iCcard.setIDRest(vIDrest);
                    iCcardDao.update3(iCcard);
                    JOptionPane.showMessageDialog(null,"修改成功");
                }
            }
        });
        pContent.add(btn3);


        breturn = new JButton("读卡");
        breturn.setBounds(270,570,70,50);
        breturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RfidTool instance = RfidTool.getInstance();
                String cardID = instance.showCardId();//获取id卡的号码
                Iccard.setText(cardID);
            }
        });
        pContent.add(breturn);

        btnOK = new JButton("重置");
        btnOK.setBounds(360,570,70,50);
        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                IcID.setText("");
                Icstatu.setText("");
                IcDate.setText("");
                IcDstr.setText("");
                cardCreat.setText("");
                Iccard.setText("");
                IDrest.setText("");
            }
        });
        pContent.add(btnOK);

        jframe.setVisible(true);

    }

//    public static void main(String[] args) {
//        Result result = new Result();
//        String id = "";
//        result.setStatus("1");
//        result.setMessage(id);
//        new IcCardManagement(result);
//    }
}
